package com.jiazhong.basic.pack0316.thread;

import java.util.concurrent.FutureTask;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

// 测试类
public class App {

    private static void a() {
        // 新建状态：产生Thread类或者子类的对象时
        DemoA a = new DemoA();
        a.setName("我是线程A"); // 给线程起了一个名字
        // 就绪状态：我准备好了，但是等待CPU执行
        a.start();
    }

    private static void b() {
        DemoB b1 = new DemoB();
        DemoB b2 = new DemoB();
        b1.start();
        b2.start();
        // 如果调用run方法，就是方法调用，不是多线程的交替执行
        // b1.run();
        // b2.run();
    }

    private static void c() {
        // 不是新建状态  c1是一个任务
        DemoC c1 = new DemoC();
        DemoC c2 = new DemoC();
        // 产生了一个线程，这个线程去执行任务c1
        Thread t1 = new Thread(c1);
        Thread t2 = new Thread(c2);
        t1.start();
        t2.start();
    }

    private static void d() throws Exception {
        // 任务
        DemoD d1 = new DemoD();
        DemoD d2 = new DemoD();
        // 任务计划
        FutureTask<Integer> ft1 = new FutureTask<>(d1);
        FutureTask<Integer> ft2 = new FutureTask<>(d2);
        // 线程
        Thread t1 = new Thread(ft1);
        Thread t2 = new Thread(ft2);
        // 线程执行任务
        t1.start();
        t2.start();
        // 获取到返回结果
        Integer result1 = ft1.get();
        Integer result2 = ft1.get();
        System.out.println(result1);
        System.out.println(result2);

    }


    private static void e1(){
        DemoE e = new DemoE();
        DemoE1 e11 = new DemoE1(e);
        DemoE1 e12 = new DemoE1(e);
        Thread t1 = new Thread(e11);
        Thread t2 = new Thread(e12);
        t1.start();
        t2.start();
    }

    private static void e2(){
        DemoE e = new DemoE();
        DemoE2 e21 = new DemoE2(e);
        DemoE2 e22 = new DemoE2(e);
        Thread t1 = new Thread(e21);
        Thread t2 = new Thread(e22);
        t1.start();
        t2.start();
    }

    private static void f(){
        Lock lock = new ReentrantLock();
        DemoF f = new DemoF(lock);
        DemoF1 f11 = new DemoF1(f);
        DemoF1 f12 = new DemoF1(f);
        Thread t1 = new Thread(f11);
        Thread t2 = new Thread(f12);
        t1.start();
        t2.start();
    }
    public static void main(String[] args) throws Exception {
        f();
    }
}
